author.js ➔ ???   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 1
1
'use strict';
2
3
const mongoose = require('mongoose');
4
const Schema = mongoose.Schema;
5
6
// Author Schema definition
7
8
const AuthorSchema = new Schema({
9
    name: {
10
        type: String,
11
        required: true
12
    },
13
    birth_year: {
14
        type: Number,
15
        required: true,
16
    },
17
    bio: {
18
        type: String
19
    },
20
    country: {
21
        type: String
22
    },
23
    createdAt: {
24
        type: Date,
25
        default: Date.now
26
    }
27
}, {
28
    versionKey: false
29
});
30
31
// Set the createdAt parameter equal to the current time
32
AuthorSchema.pre('save', next => {
33
    const now = new Date();
34
    if(!this.createdAt) {
35
        this.createdAt = now;
36
    }
37
    next();
38
});
39
40
// Exports the AuthorSchema for use elsewhere
41
module.exports = mongoose.model('author', AuthorSchema);